<template>
	<div class="system-dept-container">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
        <el-form :inline="true">
    
 
				  <el-form-item>
          
            <el-button size="default" type="success" class="ml10" @click="onOpenAddDept" v-if="!user_shop_id">
              <el-icon>
                <ele-FolderAdd />
              </el-icon>
              新增店铺
            </el-button>
          </el-form-item>
        </el-form>
      </div>
			<el-table
				:data="tableData.data"
				style="width: 100%"
				row-key="id"
				default-expand-all
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column prop="name" label="店铺名称" show-overflow-tooltip> </el-table-column>
				
				<el-table-column label="操作" width="200">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenAddDept(scope.row)" v-if="!user_shop_id">新增</el-button>
						<el-button size="small" text type="primary" @click="onOpenEditDept(scope.row)" v-if="!user_shop_id">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)" v-if="!user_shop_id">删除</el-button>
						<el-button size="small" text type="warning" @click="onOpenShopLog(scope.row)" >记录</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<Edit ref="editRef" @list="initList"/>
		<Shop ref="shopRef" />

	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent,getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import Edit from '/@/views/zmm/shop/component/edit.vue';
import {destory, getList} from "/@/api/zmm/shop";
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import Shop from '/@/views/zmm/shop/component/money_log.vue';
import { UserInfosState } from '../../../stores/interface/index';


// 定义接口来定义对象的类型
interface TableDataRow {
  id:number;
  up_id:number;
  name:string;
	children?:TableDataRow[];
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
      name:string;
		};
	};
	user_shop_id:number;
}

export default defineComponent({
	name: 'zmm_shop',
	components: { Edit ,Shop},
	setup() {
    const {proxy} = getCurrentInstance() as any;
		const editRef = ref();
		const shopRef = ref();
		const stores = useUserInfo();
		const { userInfos } = storeToRefs(stores);

		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
          name:'',
				},
			},
			user_shop_id:0,
		});
		// 初始化表格数据
		const initTableData = () => {
			state.user_shop_id = userInfos.value.shopId;
			console.log(state.user_shop_id, userInfos.value.shop_id, userInfos)
			initList();
		};
    const initList = ()=>{
      getList(state.tableData.param).then((res:any)=>{
        state.tableData.data = proxy.handleTree(res.data.zmm_shop_list??[], "id","up_id");
      });
    };
		// 打开新增菜单弹窗
		const onOpenAddDept = (row?: TableDataRow) => {
      editRef.value.openDialog(row?.id);
		};
		// 打开编辑菜单弹窗
		const onOpenEditDept = (row: TableDataRow) => {
			editRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除：${row.name}, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
          destory(row.id).then(()=>{
					  ElMessage.success('删除成功');
            initList();
          })
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		const onOpenShopLog = (row: TableDataRow) => {
		// state.tableData.memberListShowVisiable = true;
		shopRef.value.openDialog(row.id);
		console.log(shopRef)
		// memberRef.value.params.member_id = row.id


		};
		return {
			editRef,
      initList,
			onOpenAddDept,
			onOpenEditDept,
			onTabelRowDel,
			...toRefs(state),
			onOpenShopLog,
			shopRef,
		
			
		};
	},
});
</script>
